using System;
using System.Windows.Forms;

namespace Curriculum.UI.Clases
{
	/// <summary>
	/// Summary description for DateTimePickerNullable.
	/// </summary>
	public class DateTimePickerNullable : DateTimePicker
	{
		private bool isNull = false;

		private string customFormat = null;
		private DateTimePickerFormat format = DateTimePickerFormat.Long;
		private System.Drawing.Color backGroundColor;

		public System.Drawing.Color BackGroundColor
		{
			get { return backGroundColor; }
			set { backGroundColor = value; }
		}

		public DateTimePickerNullable() : base()
		{}

		public object Valor
		{
			get
			{
				if (isNull)
				{
					return null;
				}
				else
				{
					return base.Value;
				}
			}
			set
			{
				if (value == null)
				{
					if (!isNull)
					{
						format = this.Format;
						customFormat = this.CustomFormat;
						isNull = true;
					}
					this.Format = DateTimePickerFormat.Custom;
					this.CustomFormat = " ";
				}
				else
				{
					if (isNull)
					{
						this.Format = this.format;
						this.CustomFormat = this.customFormat;
						isNull = false;
					}
					base.Value = ((DateTime)value);
				}
			}
		}

		protected override void OnCloseUp(EventArgs eventargs)
		{
			if (Control.MouseButtons == MouseButtons.None)
			{
				if (isNull)
				{
					this.Format = this.format;
					this.CustomFormat = this.customFormat;
					isNull = false;                   
				}
			}
			base.OnCloseUp(eventargs);
		}
      
		protected override void OnKeyDown(KeyEventArgs e)
		{
			base.OnKeyDown(e);
			if (e.KeyCode ==Keys.Delete)
			{
				this.Valor = null;
			}
		}
	}

}
